/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 音乐搜索功能集成测试
 * 测试搜索功能的完整流程
 */

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:music_example/viewmodels/music_search_view_model.dart';
import 'package:music_example/examples/music_search_example.dart';

void main() {
  group('音乐搜索集成测试', () {
    setUp(() {
      // 设置测试环境
      SharedPreferences.setMockInitialValues({});
    });

    testWidgets('搜索页面基本功能测试', (WidgetTester tester) async {
      // 构建测试应用
      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider(
            create: (_) => MusicSearchViewModel(),
            child: const MusicSearchExample(),
          ),
        ),
      );

      // 验证页面基本元素
      expect(find.text('音乐搜索示例'), findsOneWidget);
      expect(find.byType(TextField), findsOneWidget);
      expect(find.text('搜索'), findsOneWidget);

      // 验证搜索输入框
      final searchField = find.byType(TextField);
      expect(searchField, findsOneWidget);

      // 输入搜索关键词
      await tester.enterText(searchField, '测试搜索');
      await tester.pump();

      // 验证输入内容
      expect(find.text('测试搜索'), findsOneWidget);

      // 点击搜索按钮
      await tester.tap(find.text('搜索'));
      await tester.pump();

      // 由于没有真实的网络请求，这里主要验证UI状态变化
      // 在实际应用中，这里会显示搜索结果或加载状态
    });

    testWidgets('搜索历史组件测试', (WidgetTester tester) async {
      // 创建带有历史记录的 ViewModel
      final viewModel = MusicSearchViewModel();
      viewModel.setHotItems([
        const SearchItem(text: '热门歌曲1'),
        const SearchItem(text: '热门歌曲2'),
      ]);

      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider.value(
            value: viewModel,
            child: const MusicSearchExample(),
          ),
        ),
      );

      // 等待组件初始化
      await tester.pump();

      // 验证热门搜索是否显示
      expect(find.text('热门搜索'), findsOneWidget);
      expect(find.text('热门歌曲1'), findsOneWidget);
      expect(find.text('热门歌曲2'), findsOneWidget);
    });

    testWidgets('清空搜索功能测试', (WidgetTester tester) async {
      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider(
            create: (_) => MusicSearchViewModel(),
            child: const MusicSearchExample(),
          ),
        ),
      );

      // 输入搜索内容
      final searchField = find.byType(TextField);
      await tester.enterText(searchField, '测试内容');
      await tester.pump();

      // 验证清除按钮出现
      expect(find.byIcon(Icons.clear), findsOneWidget);

      // 点击清除按钮
      await tester.tap(find.byIcon(Icons.clear));
      await tester.pump();

      // 验证输入框已清空
      final textField = tester.widget<TextField>(searchField);
      expect(textField.controller?.text, isEmpty);
    });

    testWidgets('搜索状态显示测试', (WidgetTester tester) async {
      final viewModel = MusicSearchViewModel();

      await tester.pumpWidget(
        MaterialApp(
          home: ChangeNotifierProvider.value(
            value: viewModel,
            child: const MusicSearchExample(),
          ),
        ),
      );

      // 测试加载状态
      viewModel.setLoading();
      await tester.pump();
      expect(find.text('搜索中...'), findsOneWidget);
      expect(find.byType(CircularProgressIndicator), findsOneWidget);

      // 测试空状态
      viewModel.setEmpty();
      await tester.pump();
      expect(find.text('未找到相关音乐'), findsOneWidget);
      expect(find.byIcon(Icons.search_off), findsOneWidget);

      // 测试错误状态
      viewModel.setError('网络连接失败');
      await tester.pump();
      expect(find.text('网络连接失败'), findsOneWidget);
      expect(find.byIcon(Icons.error_outline), findsOneWidget);
      expect(find.text('重试'), findsOneWidget);
    });
  });
}
